<template>
  <div class="page-container">
    <DynamicForm :config="dyConfig" :questions="dyQuestions" />
    <DynamicTable :tables="dyTable" :config="dyTableConfig" :icons="dyIcons" />
    <!-- 详细盘点结果 -->
    <InventoryResult v-model:visible="state.showDetail" v-if="state.showDetail" :show-data="state.showData" />
  </div>
</template>

<script lang="ts" setup>
import { getCurrentInstance, onBeforeUnmount, reactive } from 'vue';
import { DynamicForm } from '@/components/public-template';
import { DynamicTable } from '@/components/public-template';
import indexService from './index.service';
import { Api } from '@/config/api/statement';
import { DYNAMICLOCALICONS, DYNAMICLOCALTABLE } from '@/mixins/subject/subject-types';
import { filter } from 'rxjs/operators';
import InventoryResult from './components/inventoryResult.vue';
import { message } from 'ant-design-vue';
import { downloadFile } from '@/utils/useDownload';

const state = reactive({
  showDetail: false,
  showData: null
});
const dyConfig: any = {
  name: 'taskRecordTable',
  labelCol: 6,
  wrapperCol: 18,
  grid: 8,
  style: 'table-form',
  cancelSave: true
};
const dyTableConfig: any = {
  name: 'taskRecordTable',
  queryUrl: Api.inventoryTaskReportForm,
  protType: 'post',
  rowKey: 'index',
  isScreen: true,
  rowSelection: {
    type: 'checkbox'
  },
  beforeWithMethod: (data) => {
    if (Array.isArray(data)) {
      data.forEach((item, index) => {
        item.index = index;
      });
    }
    return data;
  }
};
const { proxy }: any = getCurrentInstance();
const dyQuestions: any = reactive(indexService.getQuestions());
const dyTable: any = reactive(indexService.getTables());
const dyIcons: any = reactive(indexService.getIcons());

// 监听table
const subscribeSub = proxy.$NotificationPubSub
  .getParams()
  .pipe(filter((res) => res !== 0))
  .subscribe((subData: any) => {
    if (subData.key === DYNAMICLOCALTABLE) {
      state.showData = subData.value.data;
      state.showDetail = true;
    } else if (subData.key === DYNAMICLOCALICONS) {
      if (subData.state.selected.length == 0) return message.warning('请先选中一个选项');
      const params = {
        list: subData.state.selected
      };
      downloadFile(Api.exportInventoryTaskReportForm, 'POST', params);
    }
  });

onBeforeUnmount(() => {
  subscribeSub && subscribeSub.unsubscribe();
  proxy.$NotificationPubSub.clearParams();
});
</script>

<style lang="less" scoped></style>
